package com.g4.shoppingsingle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.g4.shoppingsingle.common.Result;
import com.g4.shoppingsingle.dto.CartInfoDTO;
import com.g4.shoppingsingle.mapper.DdKindTypeMapper;
import com.g4.shoppingsingle.pojo.DdCartDetail;
import com.g4.shoppingsingle.mapper.DdCartDetailMapper;
import com.g4.shoppingsingle.pojo.DdKindType;
import com.g4.shoppingsingle.pojo.DdUsersInfo;
import com.g4.shoppingsingle.service.IDdCartDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingsingle.service.IDdUsersInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-27
 */
@Service
public class DdCartDetailServiceImpl extends ServiceImpl<DdCartDetailMapper, DdCartDetail> implements IDdCartDetailService {

    @Autowired
    private IDdUsersInfoService iDdUsersInfoService;

    @Autowired(required = false)
    private DdCartDetailMapper ddCartDetailMapper;

    @Autowired(required = false)
    private DdKindTypeMapper ddKindTypeMapper;

    /**
     * 获取当前登录用户的购物车信息
     * @return
     */
    @Override
    public List<CartInfoDTO> selectList() {
        //1、获取当前登录的用户
        DdUsersInfo me = iDdUsersInfoService.getMe();
        //2、根据用户id查询购物车
        //通过用户id去查询购物车信息
        List<CartInfoDTO> ddCartDetails = ddCartDetailMapper.selectCartByUserId(me.getId());
        return ddCartDetails;
    }

    /**
     * 添加购物车
     * @param typeId
     * @param productId
     */
    @Override
    public Result addCart(Integer typeId, Integer productId) {
        //1、获取商品的具体配置
        DdKindType ddKindType = ddKindTypeMapper.selectById(typeId);
        //2、获取当前登录用户
        DdUsersInfo me = iDdUsersInfoService.getMe();
        //3、添加购物车
        DdCartDetail pojo = new DdCartDetail();
        pojo.setNum(1);     // TODO 添加购物车数量
        pojo.setColor(ddKindType.getColor());
        pojo.setPdId(productId);
        pojo.setPdName(ddKindType.getPdName());
        pojo.setPdVersion(ddKindType.getPdVersion());
        pojo.setTypeId(typeId);
        pojo.setUserId(me.getId());
        ddCartDetailMapper.insert(pojo);
        return Result.ok();
    }
}
